package com.aos.eap.fragment;

import android.graphics.drawable.Drawable;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.aos.eap.R;
import com.aos.eap.activity.SettingActivity;
import com.aos.eap.base.Constant;
import com.aos.eap.bean.EventMessageBean;
import com.aos.eap.utils.MyUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.pda.platform.ui.ui_pdaplatform.adapter.FreeUI_TabIndexGridAdapter;
import com.pda.platform.ui.ui_pdaplatform.base.FreeUI_BaseFragment;
import com.pda.platform.ui.ui_pdaplatform.entity.FreeUI_AddViewEntity;
import com.pda.platform.ui.ui_pdaplatform.entity.FreeUI_TabIndexGridEntity;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_Utils;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.List;

import androidx.appcompat.widget.Toolbar;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import butterknife.BindView;
import butterknife.OnClick;

public class IndexFragment extends FreeUI_BaseFragment {


    @BindView(R.id.ivSetting)
    ImageView ivSetting;
    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.rvMFG)
    RecyclerView rvMFG;
    @BindView(R.id.llMFG)
    LinearLayout llMFG;
    @BindView(R.id.rvPE)
    RecyclerView rvPE;
    @BindView(R.id.llPE)
    LinearLayout llPE;
    @BindView(R.id.rvEE)
    RecyclerView rvEE;
    @BindView(R.id.llEE)
    LinearLayout llEE;
    @BindView(R.id.rvQc)
    RecyclerView rvQc;
    @BindView(R.id.llQC)
    LinearLayout llQC;
    @BindView(R.id.tvTitle)
    TextView tvTitle;
    public IndexFragment() {
        // Required empty public constructor
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        EventBus.getDefault().unregister(this);
    }

    @Override
    protected int getLayoutResId() {
        return R.layout.fragment_index;
    }

    @Override
    protected void initView() {
        EventBus.getDefault().register(this);
        rvMFG.setLayoutManager(new GridLayoutManager(getActivity(), 3));
        rvPE.setLayoutManager(new GridLayoutManager(getActivity(), 3));
        rvEE.setLayoutManager(new GridLayoutManager(getActivity(), 3));
        rvQc.setLayoutManager(new GridLayoutManager(getActivity(), 3));
    }

    @Override
    protected void initData() {
        initAll();
    }

    @Override
    protected FreeUI_AddViewEntity getBaseEntity() {
        return null;
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void loadPage(EventMessageBean eventMessageBean) {
        initAll();
    }

    private void initAll() {
        tvTitle.setText("首页\n"+Constant.CURRENT_USER);
        initMFGArea();
        initPEArea();
        initEEArea();
        initQCArea();
    }

    private void initMFGArea() {
        if (Constant.CURRENT_LOGINBEAN.getMfg_list().size() > 0) {
            llMFG.setVisibility(View.VISIBLE);
            FreeUI_TabIndexGridAdapter tabIndexGridAdapter = new FreeUI_TabIndexGridAdapter(
                    setPicToDrawable(Constant.CURRENT_LOGINBEAN.getMfg_list()), R.layout.free_ui_glory_adapter_tab_index_grid);
            tabIndexGridAdapter.openLoadAnimation(BaseQuickAdapter.ALPHAIN);
            tabIndexGridAdapter.isFirstOnly(true);
            rvMFG.setAdapter(tabIndexGridAdapter);
        } else {
            llMFG.setVisibility(View.GONE);
        }
    }

    private void initPEArea() {
        if (Constant.CURRENT_LOGINBEAN.getPe_list().size() > 0) {
            llPE.setVisibility(View.VISIBLE);
            FreeUI_TabIndexGridAdapter tabIndexGridAdapter = new FreeUI_TabIndexGridAdapter(
                    setPicToDrawable(Constant.CURRENT_LOGINBEAN.getPe_list()), R.layout.free_ui_glory_adapter_tab_index_grid);
            tabIndexGridAdapter.openLoadAnimation(BaseQuickAdapter.ALPHAIN);
            tabIndexGridAdapter.isFirstOnly(true);
            rvPE.setAdapter(tabIndexGridAdapter);
        } else {
            llPE.setVisibility(View.GONE);
        }
    }

    private void initEEArea() {
        if (Constant.CURRENT_LOGINBEAN.getEe_list().size() > 0) {
            llEE.setVisibility(View.VISIBLE);
            FreeUI_TabIndexGridAdapter tabIndexGridAdapter = new FreeUI_TabIndexGridAdapter(
                    setPicToDrawable(Constant.CURRENT_LOGINBEAN.getEe_list()), R.layout.free_ui_glory_adapter_tab_index_grid);
            tabIndexGridAdapter.openLoadAnimation(BaseQuickAdapter.ALPHAIN);
            tabIndexGridAdapter.isFirstOnly(true);
            rvEE.setAdapter(tabIndexGridAdapter);
        } else {
            llEE.setVisibility(View.GONE);
        }
    }

    private void initQCArea() {
        if (Constant.CURRENT_LOGINBEAN.getQc_list().size() > 0) {
            llQC.setVisibility(View.VISIBLE);
            FreeUI_TabIndexGridAdapter tabIndexGridAdapter = new FreeUI_TabIndexGridAdapter(
                    setPicToDrawable(Constant.CURRENT_LOGINBEAN.getQc_list()), R.layout.free_ui_glory_adapter_tab_index_grid);
            tabIndexGridAdapter.openLoadAnimation(BaseQuickAdapter.ALPHAIN);
            tabIndexGridAdapter.isFirstOnly(true);
            rvQc.setAdapter(tabIndexGridAdapter);
        } else {
            llQC.setVisibility(View.GONE);
        }
    }

    private List<FreeUI_TabIndexGridEntity> setPicToDrawable(List<FreeUI_TabIndexGridEntity> entity) {
        for (FreeUI_TabIndexGridEntity og : entity) {
            if (og.getPicName().length() != 0) {
                Drawable drawable = getResources()
                        .getDrawable(getResources()
                                .getIdentifier(og.getPicName(), "drawable", getActivity().getPackageName()));
                og.setDrawable(drawable);
            }
        }
        return entity;
    }

    @OnClick(R.id.ivSetting)
    public void onViewClicked() {
        if (MyUtils.isFastClick()){
            showToast("您点击的太快了",false);
            return;
        }
        FreeApi_Utils.skipWithExtraIntent("header", getActivity(), SettingActivity.class);
    }
}
